<template>
  <div class="main">
    <img class="headImg" src="../../assets/image/discount/head.png" alt="" />

    <div class="mainbox">
      <div
        class="producterBox link_title"
        v-for="(obj, objIndex) in arr"
        :key="objIndex"
        :id="'box' + objIndex"
      >
        <div class="title">
          <img class="bg" src="../../assets/image/discount/desbg.png" alt="" />
          <div class="firebox">
            <img
              v-for="(item, index) in objIndex + 1"
              :key="index"
              class="fire"
              src="../../assets/image/discount/fire.png"
              alt=""
            />
          </div>
          <img
            class="pricebg"
            v-if="objIndex == 0"
            src="../../assets/image/discount/topprice.png"
            alt=""
          />
          <img
            class="pricebg"
            v-if="objIndex == 1"
            src="../../assets/image/discount/mediumprice.png"
            alt=""
          />
          <img
            class="pricebg"
            v-if="objIndex == 2"
            src="../../assets/image/discount/bottomprice.png"
            alt=""
          />
          <!-- <div class="lookmore">查看更多 >></div> -->
        </div>

        <div class="clothesbox">
          <div
            class="clothes"
            v-for="(item1, index) in arr[objIndex]"
            :key="index"
          >
            <clothes :item="item1"></clothes>
          </div>
        </div>
      </div>

      <div class="maodianbox">
        <img src="../../assets//image/discount/maodian.png" alt="" />
        <div class="itembox">
          <div
            class="item"
            :class="activeIndex == 0 ? 'activeitem' : ''"
            @click="toView(0)"
          >
            0-20元专区
          </div>
          <div
            class="item"
            :class="activeIndex == 1 ? 'activeitem' : ''"
            @click="toView(1)"
          >
            20-30元专区
          </div>
          <div
            class="item"
            :class="activeIndex == 2 ? 'activeitem' : ''"
            @click="toView(2)"
          >
            30-40元专区
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import clothes from "./components/clothes.vue";
import { selectSpecialProduct } from "@/api/home";
export default {
  components: {
    clothes,
  },

  data() {
    return {
      arr: [
        {
          img: require("../../assets/image/discount/topprice.png"),
          num: 3,
        },
        {
          img: require("../../assets/image/discount/mediumprice.png"),
          num: 2,
        },
        {
          img: require("../../assets/image/discount/bottomprice.png"),
          num: 1,
        },
      ],
      activeIndex: 0,
    };
  },
  activated() {
    this.selectSpecialProduct();
  },
  methods: {
    //获取清仓列表
    selectSpecialProduct() {
      selectSpecialProduct().then((data) => {
        this.arr = data.data;
      });
    },
    toView(index) {
      this.activeIndex = index;
      // document.querySelector(`#box${index}`).scrollIntoView(true);
      this.scrollToLinkTitle(index);
    },
    scrollToLinkTitle(index) {
      // 获取含样式类 class="link_title" 的所有元素，得到一个数组（列表）
      let titleList = document.querySelectorAll(".link_title");
      console.log(titleList);
      // offsetTop为元素顶部与整个文档顶部间的距离
      let offsetTop = titleList[index].offsetTop;
      // offset为平滑滚动结束后，元素顶部与显示屏顶部间的距离,默认为0，页面滚动到标题触顶
      let offset = -700;
      offsetTop -= offset;
      // scrollTop为显示屏顶部与整个文档顶部间的距离
      let scrollTop = document.querySelector("#app").scrollTop;
      // 步长（时长500ms，每10ms一跳，共50跳）
      let step = Math.abs(offsetTop - scrollTop) / 50;
      if (offsetTop > scrollTop) {
        // offsetTop > scrollTop 时元素在显示屏下方时，滚动条需下滑
        // scrollTop需增加（offsetTop-scrollTop）
        smoothDown();
      } else {
        // offsetTop < scrollTop 时元素在显示屏上方时，滚动条需上滑
        // scrollTop需减少（scrollTop - offsetTop）
        smoothUp();
      }
      // 向上平滑滚动
      function smoothUp() {
        if (scrollTop > offsetTop) {
          scrollTop -= step;
          document.querySelector("#app").scrollTop = scrollTop;
          document.querySelector("#app").scrollTop = scrollTop;
          setTimeout(smoothUp, 10);
        } else {
          document.querySelector("#app").scrollTop = offsetTop;
          document.querySelector("#app").scrollTop = offsetTop;
        }
      }
      // 向下平滑滚动
      function smoothDown() {
        if (scrollTop < offsetTop) {
          scrollTop += step;
          document.querySelector("#app").scrollTop = scrollTop;
          document.querySelector("#app").scrollTop = scrollTop;
          setTimeout(smoothDown, 10);
        } else {
          document.querySelector("#app").scrollTop = offsetTop;
          document.querySelector("#app").scrollTop = offsetTop;
        }
      }
    },
  },
};
</script>

<style scoped lang="scss">
.main {
  width: 100%;
  // height: 100%;
  background-color: #f5f7f9;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  .headImg {
    width: 100%;
    height: auto;
  }
  .mainbox {
    margin: 0rem auto;
    padding: 0.4rem 0rem;
    width: 100%;
    background: #ffffff;
    min-height: 10rem;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    position: relative;
    .producterBox:first-child {
      margin-top: 0rem;
    }
    .producterBox {
      margin-top: 0.4rem;
      padding: 0.47rem 0.69rem;
      width: 12.02rem;
      min-height: 7.66rem;
      border-radius: 0.42rem;
      background: rgba(255, 234, 231, 0.64);
      display: flex;
      flex-direction: column;
      // justify-content: space-between;
      align-items: center;
      .title {
        width: 8rem;
        height: 0.8323rem;
        position: relative;
        .bg {
          width: 100%;
          height: 100%;
          position: absolute;
          top: 0;
          left: 0;
        }
        .firebox {
          width: 1.0478rem;
          height: auto;
          display: flex;
          justify-content: space-around;
          position: absolute;
          top: 0.2382rem;
          left: 1.2543rem;
          .fire {
            width: 0.28rem;
            height: auto;
          }
        }
        .pricebg {
          width: 1.4rem;
          height: auto;
          position: absolute;
          top: 0.3rem;
          left: 2.88rem;
        }
        .lookmore {
          font-size: 0.16rem;
          color: #ffffff;
          position: absolute;
          top: 0.32rem;
          left: 5.83rem;
          cursor: pointer;
        }
      }
      .clothesbox {
        width: 100%;
        min-height: 6.4rem;
        display: flex;
        margin-top: 20px;
        // justify-content: space-between;
        // align-content: center;
        // align-items: center;
        flex-wrap: wrap;
        .clothes:nth-child(-n + 5) {
          margin-top: 0rem;
        }
        .clothes {
          margin-top: 0.2rem;
          width: 2.24rem;
          height: 3.1rem;
          border-radius: 0.12rem;
          background: #fcfcf2;
          margin-right: 0.16rem;
        }
      }
    }
    .maodianbox {
      width: 1.4rem;
      height: 2.64rem;
      position: fixed;
      bottom: 3rem;
      right: 2.19rem;
      img {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
      }
      .itembox {
        width: 1.14rem;
        height: 1rem;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        position: absolute;
        bottom: 0.3rem;
        left: 50%;
        transform: translateX(-50%);
        .item {
          width: 100%;
          height: 0.32rem;
          line-height: 0.32rem;
          text-align: center;
          font-size: 0.12rem;
          color: #ffffff;
          cursor: pointer;
        }
        .activeitem {
          background: #ffffff;
          border-radius: 0.19rem;
          color: #ff0036;
        }
      }
    }
  }
}
</style>
